//
//  UIDevice+Ai.swift
//  ArtAIClassSwift
//
//  Created by mal on 2019/11/11.
//  Copyright © 2019 meishubao.com. All rights reserved.
//

import Foundation
import UIKit

public enum MLDeviceType {
    case MLiPad
    case MLiPhone
    case MLiPod
    case MLUnknown
    static func deviceType() -> MLDeviceType {
        let deviceType = UIDevice.current.model
        switch deviceType {
        case "iPad":
            return MLiPad
        case "iPhone":
            return MLiPhone
        case "iPod touch":
            return MLiPod
        default:
            return MLUnknown
        }
    }
}

public let MLIsiPad = (MLDeviceType.deviceType() == .MLiPad)
public let MLIsiPhone = (MLDeviceType.deviceType() == .MLiPhone)

public func MLKeyWindow() -> UIWindow? {
    guard let window = UIApplication.shared.delegate?.window else {
        return nil
    }
    return window
}

//MARK: safe area
public let MLSafeAreaInsets = {() -> UIEdgeInsets in
    guard #available(iOS 11.0, *) else {
        return .zero
    }
    if let keyWindow = MLKeyWindow() {
        return keyWindow.safeAreaInsets
    }
    return .zero
}

//FIXME: 因为目前只支持竖屏所以safeArea是不变的取一次就可以；如果后边支持横屏的话这里不能用let常量；
public let MLSafeTop = MLSafeAreaInsets().top
public let MLSafeBottom = MLSafeAreaInsets().bottom
public let MLTopH: CGFloat = (MLSafeTop == 0) ? (20) : MLSafeTop
public let MLBottomBarHeight: CGFloat = MLSafeBottom + 49

/// 根据当前机型返回对应传入的参数(iPad/iPhone)
/// - Parameter iPhone: iPhone参数
/// - Parameter iPad: iPad参数
public func MLDeviceValue<T>(iPhone: T, iPad: T, smallScreen: T? = nil) -> T {
    if MLIsiPad {
        return iPad
    } else if MLIsiPhone {
        return iPhone
    } else if ScreenWidth == 320, let small = smallScreen {
        return small
    }
    return iPhone
}

/// 系统版本号
public var ml_systemVersion: Float {
    if let version = Float(UIDevice.current.systemVersion) {
        return version
    }
    return 0.0
}

//public extension UIDevice {
//    static func ml_callPhoneWith(phoneNum: String?) {
//        if let phone = phoneNum, phone.count > 0 {
//            let phoneStr = phone.replace(" ", new: "")
//            let phoneNum = "telprompt://\(phoneStr)"
//            if let url = URL(string: phoneNum) {
//                UIApplication.shared.open(url)
//            }
//        }
//    }
//}
